< previous page page_303 next page >

Page 303
Look carefully at the values themselves. They are all hex values. Picture the values in binary (you should be able to do this in your head, but feel free to look at the tables in Tutorial 2 if you need to). You should see
Public Const SWP_NOSIZE =     &H1 =   0000000000000001
Public Const SWP_NOMOVE =     &H2 =   0000000000000010
Public Const SWP_NOZORDER =   &H4 =   0000000000000100
Public Const SWP_NOACTIVATE = &H10 =  0000000000010000
Public Const SWP_SHOWWINDOW = &H40 =  0000000001000000
Public Const SWP_HIDEWINDOW = &H80 =  0000000010000000
Public Const SWP_NOCOPYBITS = &H100 = 0000000100000000
See it now? Each of the constants represents a number where a single bit is set. Not every bit has a constant in this listing, but that's because only a partial list is shown here to conserve space. A 32-bit long parameter can have up to 32 options specified in this way, one for each bit. The uFlags parameter is a 32-bit value, though only 16 bits are shown in the listing above.
Let's say you wanted to ignore the move parameters, ignore the size parameters, and hide the window without changing its position in the Z-order. To do this, you would need to combine the SWP_NOSIZE, SWP_NOMOVE, SWP_NOZORDER, and SWP_HIDEWINDOW constants.
What happens if you combine them using the AND operator?
7017a6ead0e3c4111b47a554df321e9f.gif
&H1 And &H2 And &H4 And &H80 = 0
because for each pair of bits, 1 AND 0 = 0.
But if you use the OR operator, you get the following:
SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_HIDEWINDOW ->
&H1 Or &H2 Or &H4 Or &H80 -> &H87 -> 000010000111 in binary.
Look at the binary values of each constant and the binary value of the result. The OR operator does, in effect, combine the individual bits.
Why take this approach for passing instructions to a function? Because if you used a separate parameter for each option, you would end up with functions that have dozens of parameters. And each parameter requires not only extra memory space when the function is called, but extra CPU operations to store the parameter on the stack when the function is called. Using individual bits in parameters in this manner is extremely efficient. By specifying constants that represent single bits, it is possible to precisely set any combination of bits using the OR operator. Any parameter or variable whose meaning is defined by individual bits that have separate definitions is called a flag variable, and the individual bits are called flag bits.

 
< previous page page_303 next page >